﻿using Microsoft.Extensions.Logging;
using StarsAdmin.Core.Extensions;

namespace StarsAdmin.Core.Helpers
{
    public class LogHelper
    {
        private static readonly ILogger _logger = LoggerFactory.Create(builder => builder.AddLog4Net(new Log4NetProviderOptions() { Log4NetConfigFileName = "Log4net.config" })).CreateLogger("");

        public static void Trace(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                _logger.LogTrace(msg.ParseToString());
            } else
            {
                _logger.LogTrace(msg + GetExceptionMessage(ex));
            }
        }

        public static void Debug(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                _logger.LogDebug(msg.ParseToString());
            } else
            {
                _logger.LogDebug(msg + GetExceptionMessage(ex));
            }
        }

        public static void Info(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                _logger.LogInformation(msg.ParseToString());
            } else
            {
                _logger.LogInformation(msg + GetExceptionMessage(ex));
            }
        }

        public static void Warn(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                _logger.LogWarning(msg.ParseToString());
            } else
            {
                _logger.LogWarning(msg + GetExceptionMessage(ex));
            }
        }

        public static void Error(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                _logger.LogError(msg.ParseToString());
            } else
            {
                _logger.LogError(msg + GetExceptionMessage(ex));
            }
        }

        public static void Error(Exception ex)
        {
            if (ex != null)
            {
                _logger.LogError(GetExceptionMessage(ex));
            }
        }

        public static void Fatal(object msg, Exception ex = null)
        {
            if (ex == null)
            {
                _logger.LogCritical(msg.ParseToString());
            } else
            {
                _logger.LogCritical(msg + GetExceptionMessage(ex));
            }
        }

        public static void Fatal(Exception ex)
        {
            if (ex != null)
            {
                _logger.LogCritical(GetExceptionMessage(ex));
            }
        }

        private static string GetExceptionMessage(Exception ex)
        {
            string message = string.Empty;
            if (ex != null)
            {
                message += ex.Message;
                message += Environment.NewLine;
                Exception originalException = ex.GetOriginalException();
                if (originalException != null)
                {
                    if (originalException.Message != ex.Message)
                    {
                        message += originalException.Message;
                        message += Environment.NewLine;
                    }
                }
                message += ex.StackTrace;
                message += Environment.NewLine;
            }
            return message;
        }
    }
}